#import "XController.h"

#import "XApp.h"
#import "XMacro.h"


@interface XController ()
@property (nonatomic, strong) NSMutableDictionary       *dicParams;
@property (nonatomic, strong) UIScrollView              *scContainer;
@property (nonatomic, strong) UIControl                 *cContainer;

@end


@implementation XController


/**
 * 设置参数列表
 * @param        params，参数列表
 * @result
 */
- (void)setParams:(NSDictionary *)params {
    VERIFY_OBJECT(params, none);
    // 删除原来的参数列表，重新设置为新添加的参数
    _dicParams = [[NSMutableDictionary alloc] initWithDictionary:params];
}


/**
 * 添加参数
 * @param        param，参数
 * @param        key，参数名
 * @result
 */
- (void)addParam:(NSObject *)param forKey:(NSString *)key {
    VERIFY_OBJECT(param, none);
    VERIFY_OBJECT(key, none);
    [_dicParams setValue:param forKey:key];
}


/**
 * 通过Key值，获取参数
 * @param        key，参数对应的Key值
 * @result       返回对应的Key值
 */
- (NSObject *)paramForKey:(NSString *)key {
    VERIFY_OBJECT(key, nil);
    return [_dicParams objectForKey:key];
}


/**
 * 显示
 * @result
 */
- (void)show {
    // 默认为立刻显示
    [XApp pushViewController:self];
}


/**
 * 注册键盘系统事件
 * @result
 */
- (void)registerKeyboardNotification {
    // 注册系统通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}


/**
 * 注册键盘系统事件
 * @result
 */
- (void)cancelRegisterKeyboardNotification {
    // 注册系统通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}


#pragma mark 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 处理窗口控件的转移
    CGRect rc = self.view.frame;
    rc.origin = CGPointZero;
    rc.size.height -= 64;
    _cContainer = [[UIControl alloc] initWithFrame:rc];
    _cContainer.backgroundColor = self.view.backgroundColor;
    [_cContainer addTarget:self action:@selector(onBackgroundTouch) forControlEvents:UIControlEventTouchUpInside];
    
    rc = self.view.frame;
    rc.origin = CGPointZero;
    rc.size.height -= 64;
    _scContainer = [[UIScrollView alloc] initWithFrame:rc];
    _scContainer.contentSize = _cContainer.frame.size;
    _scContainer.scrollEnabled = YES;
    _scContainer.backgroundColor = self.view.backgroundColor;
    
    [_scContainer addSubview:_cContainer];
    
    NSArray *arrSubViews = [self.view subviews];
    for (UIView *sub in arrSubViews) {
        [_cContainer addSubview:sub];
    }
    self.view = _scContainer;
    
    // 注册系统键盘事件
    [self registerKeyboardNotification];
}


- (void)viewWillDisappear:(BOOL)animated {
    // 注销键盘事件
    [self cancelRegisterKeyboardNotification];
    
    [super viewWillDisappear:animated];
}


- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)onBackgroundTouch {
}


#pragma mark KeyboardNotification
- (void)keyboardWillShow:(NSNotification *)noti {
    NSDictionary *usrInfo      = noti.userInfo;
    NSValue      *aValue       = [usrInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    NSValue      *durationValue = [usrInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval  animationDuration;
    [durationValue getValue:&animationDuration];
    
    CGRect       keyboardRect  = [aValue CGRectValue];
    keyboardRect               = [self.view convertRect:keyboardRect
                                               fromView:nil];
    CGRect        frame        = self.view.frame;
    frame.origin = CGPointZero;
    frame.size                 = CGSizeMake(frame.size.width, keyboardRect.origin.y);
    frame.size.height += 64;
    
    [UIView animateWithDuration:animationDuration  animations:^{
        _scContainer.frame            = frame;
    } completion:^(BOOL finished) {
    }];
}


- (void)keyboardWillHide:(NSNotification *)noti {
    NSDictionary   *usrInfo = noti.userInfo;
    NSValue        *durationValue = [usrInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval  animationDuration;
    [durationValue getValue:&animationDuration];
    
    CGRect        frame        = self.view.frame;
    frame.size = _scContainer.contentSize;
    frame.size.height += 64;
    
    [UIView animateWithDuration:animationDuration  animations:^{
        _scContainer.frame = frame;
    } completion:^(BOOL finished) {
    }];
}

@end

